package com.sskj.community.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.community.presenter.ShareHotPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.ShareBean;
import com.sskj.lib.bean.TextDetailsBean;
import com.sskj.lib.util.WXShareUtil;
import com.sskj.lib.widget.BottomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/07/17
 */
@Route(path = RConfig.COMMUNITY_ACTIVITY_SHARE)
public class ShareHotActivity extends BaseActivity<ShareHotPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.cs_share)
    ConstraintLayout csShare;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R2.id.tv_title)
    TextView tvShareTitle;
    @BindView(R2.id.tv_content)
    TextView tvContent;
    @Autowired
    String msg_id;

    private Bitmap bitmap;

    @Override
    public int getLayoutId() {
        return R.layout.community_activity_share_hot;
    }

    @Override
    public ShareHotPresenter getPresenter() {
        return new ShareHotPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.fenxiang));
        ARouter.getInstance().inject(this);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.icon_fenxiang);
    }

    @Override
    public void initData() {
        mPresenter.getTextDetails(msg_id);
        mPresenter.getShareInfo();
        ClickUtil.click(300, ivRight, () -> {
            bitmap = Bitmap.createBitmap(csShare.getWidth(), csShare.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
            csShare.draw(canvas);
            if (bitmap == null) {
                return;
            }
            BottomDialog.share(this, tv -> {
                switch (tv) {
                    case Constans.SHARE_WECHAT:
                        WXShareUtil.shareFriendsBitmap(bitmap);
                        break;
                    case Constans.SHARE_PENGYOUQUAN:
                        WXShareUtil.shareQuanBitmap(bitmap);
                        break;
                }
            });
        });
    }

    public void getTextDetails(TextDetailsBean data) {
        if (data != null) {
            tvShareTitle.setText(data.getTitle());
            tvContent.setText(data.getContent());
        }
    }

    public void getShareInfoOK(ShareBean data) {
        if (data == null) {
            return;
        }
        ImageUtil.setImageWithoutCache(data.getQrc(), ivQrcode);
    }
}
